<template>
  <div id="adPositionSearch">
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="显示终端" prop="terminalType">
          <el-select v-model="formInline.terminalType" clearable>
            <el-option
              v-for="item in terminalTypeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="位置" prop="locationType">
          <el-select v-model="formInline.locationType" clearable>
            <el-option
              v-for="item in showLocationList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="formInline.status" clearable>
            <el-option
              v-for="item in statusList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" class="long" prop="sort">
          <el-select v-model="formInline.sort" placeholder="请选择">
            <el-option
              v-for="item in sortList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="formInline.name"
            placeholder="请输入名称"
            clearable
          />
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'

export default {
  name: 'ActiveManagementSearch',
  msg: '阅读内容-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          terminalType: '', //显示终端
          locationType: '', //位置
          name: '', // 名称
          sort: '', //排序
          status: '' // 状态
        }
      }
    }
  },
  data() {
    return {
      terminalTypeList: [{ dictLabel: '全部', dictValue: '' }], // 显示终端
      showLocationList: [], // 位置(展示列表)
      allLocationList: [], // 位置(全部列表)
      pcLocationList: [], // 终端为pc的位置列表(A1/A2)
      h5LocationList: [], // 终端为H5的位置列表(c1/c2)
      appLocationList: [], // 终端为APP的位置列表
      // 状态
      statusList: [{ dictLabel: '全部', dictValue: '' }],
      // 排序
      sortList: [],
      rules: {
        terminalType: {},
        channel: {},
        locationType: {},
        name: {},
        sort: {},
        status: {}
      }
    }
  },
  watch: {
    'formInline.terminalType': {
      handler(newV, oldV) {
        if (newV === 'H5') {
          this.showLocationList = [...this.h5LocationList]
        } else if (newV === 'APP') {
          this.showLocationList = [...this.appLocationList]
        } else if (newV === 'PC') {
          this.showLocationList = [...this.pcLocationList]
        } else {
          this.showLocationList = [...this.allLocationList]
        }
        this.formInline.locationType = this.showLocationList[0].dictValue
      },
      deep: true
    }
  },
  mounted() {
    this.getStatusList()
  },
  methods: {
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      //设置默认数据
      this.setDefaultValue()
    },
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    async getStatusList() {
      // 显示终端
      let data0 = await sysNormalDisable('advertising_spaces_terminal')
      this.terminalTypeList = this.terminalTypeList.concat(data0)
      // 位置
      let pcData = await sysNormalDisable('advertising_spaces_location')
      let h5Data = await sysNormalDisable('advertising_spaces_location_H5')
      let appData = await sysNormalDisable('advertising_spaces_location_app')
      this.h5LocationList = [{ dictLabel: '全部', dictValue: '' }, ...h5Data]
      this.pcLocationList = [{ dictLabel: '全部', dictValue: '' }, ...pcData]
      this.appLocationList = [{ dictLabel: '全部', dictValue: '' }, ...appData]
      this.allLocationList = [
        { dictLabel: '全部', dictValue: '' },
        ...pcData,
        ...h5Data,
        ...appData
      ]
      this.showLocationList = [...this.allLocationList]
      // 状态
      let data1 = await sysNormalDisable(
        'advertising_spaces_status',
        'adPositionStatus'
      )
      this.statusList = this.statusList.concat(data1)
      // 排序规则
      this.sortList = await sysNormalDisable('advertising_spaces_sort')
      this.setDefaultValue()
      //触发获取列表
      this.$emit('toSearch')
    },
    /*设置默认数据*/
    setDefaultValue() {
      this.formInline.terminalType = this.terminalTypeList[0].dictValue
      this.formInline.locationType = this.showLocationList[0].dictValue
      this.formInline.status = this.statusList[0].dictValue
      this.formInline.sort = this.sortList[0].dictValue
    }
  }
}
</script>

<style lang="less">
#adPositionSearch {
  .long {
    .el-input {
      &.el-input--suffix {
        width: 130px !important;
      }
    }
  }
  .el-input.el-input--suffix {
    .el-input__inner {
      padding-right: 25px !important;
    }
  }
}
</style>
